package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LiangTianQi
 * @describe
 * @date 2020/11/22 13:48
 */
@Service(interfaceClass = ReportService.class)
public class RepotServiceImpl implements ReportService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    /**
     * 封装了运营统计报表所需的数据
     * @return
     */
    @Override
    public Map<String, Object> getBusinessReport() {
        //获取当前日期月份的第一天
        LocalDate now = LocalDate.now();
        LocalDate localDate = LocalDate.of(now.getYear(), now.getMonth(), 1);
        String months = localDate.format(DateTimeFormatter.ofPattern("yyyy.MM.dd"));
        //获取当前日期星期的第一天
        LocalDate localDate1 = now.with(DayOfWeek.MONDAY);
        String week = localDate1.format(DateTimeFormatter.ofPattern("yyyy.MM.dd"));

        //会员数据统计
        //报表日期
        String  reportDate= now.format(DateTimeFormatter.ofPattern("yyyy.MM.dd"));
        //获取会员总数
        Integer totalMember= memberDao.getCount();
        //获取今日新增会员数
        Integer todayNewMember= memberDao.getCountByDate(reportDate);
        //获取本周新增会员数
        Integer thisWeekNewMember=memberDao.getCountByMonthOrWeek(week);
        //获取本月新增会员数
        Integer thisMonthNewMember=memberDao.getCountByMonthOrWeek(months);

        //预约到诊数据统计
        //今日预约
        Integer todayOrderNumber= orderDao.getOrderCountByDate(reportDate);
        //本周预约数
        Integer thisWeekOrderNumber=orderDao.getOrderCountByMonthOrWeek(week);
        //本月预约数
        Integer thisMonthOrderNumber=orderDao.getOrderCountByMonthOrWeek(months);
        //今日到诊数
        Integer todayVisitsNumber= orderDao.getOrderVisitCountByDate(reportDate);
        //本周到诊数
        Integer thisWeekVisitsNumber= orderDao.getOrderVisitCountByDate(reportDate);
        //本月到诊数
        Integer thisMonthVisitsNumber= orderDao.getOrderVisitCountByDate(reportDate);

        //热门套餐
        //将套餐名称，预约数量，占比封装到list<map>集合中
        List<Map<String, Object>> hotSetmeal=orderDao.getHotmeal();

        //封装到map集合中
        Map<String, Object> map = new HashMap<>();
        map.put("reportDate",reportDate);
        map.put("totalMember",totalMember);
        map.put("todayNewMember",todayNewMember);
        map.put("thisMonthNewMember",thisMonthNewMember);
        map.put("thisWeekNewMember",thisWeekNewMember);
        map.put("todayOrderNumber",todayOrderNumber);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);
        map.put("todayVisitsNumber",todayVisitsNumber);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        map.put("hotSetmeal",hotSetmeal);
        return map;
    }

}
